import {
    createMemoryHistory,
    createWebHashHistory,
    createRouter,
    createWebHistory
} from 'vue-router'
import {useConfigStore} from '@/stores/config'
import {useUserStore} from '@/stores/user'
import {useLocalConfigStore} from '@/stores/localConfig'
const errorPages = [
    {
        path: '401',
        component: () => import('pages/error/401.vue'),
        meta: {title: 'LaravelAdmin-授权错误页面'}
    },
    {
        path: '403',
        component: () => import('pages/error/403.vue'),
        meta: {title: 'LaravelAdmin-权限不足'}
    },
    {
        path: '500',
        component: () => import('pages/error/500.vue'),
        meta: {title: 'LaravelAdmin-服务器内部错误'}
    },
    {
        path: '/:pathMatch(.*)*', // Vue Router 4.x的写法，匹配所有路径
        name: '404',
        component: () => import('pages/error/404.vue'),
        meta: {title: 'LaravelAdmin-页面不存在'}
    }
];
const routes = [
    {
        path: '/guest',
        component: () => import('pages/guest/layout.vue'),
        children: [
            {
                path: '',
                component: () => import('pages/guest/login/password.vue'),
                meta: {title: 'LaravelAdmin-密码登录'}
            },
            {
                path: 'login',
                name: 'login',
                component: () => import('pages/guest/login/password.vue'),
                meta: {title: 'LaravelAdmin-密码登录'}
            },
            {
                path: 'login/password',
                component: () => import('pages/guest/login/password.vue'),
                meta: {title: 'LaravelAdmin-密码登录'}
            },
            {
                path: 'login/code',
                component: () => import('pages/guest/login/code.vue'),
                meta: {title: 'LaravelAdmin-验证码登录'}
            },
            {
                path: 'register',
                name: 'register',
                component: () => import('pages/guest/register.vue'),
                meta: {title: 'LaravelAdmin-注册'},
                children: [
                    {
                        path: '',
                        component: () => import('pages/guest/register/step0.vue'),
                        meta: {title: 'LaravelAdmin-注册-第一步'},
                    },
                     {
                        path: 'step1',
                        component: () => import('pages/guest/register/step1.vue'),
                        meta: {title: 'LaravelAdmin-注册-第二步'},
                    },
                     {
                        path: 'step2',
                        component: () => import('pages/guest/register/step2.vue'),
                        meta: {title: 'LaravelAdmin-注册-第三步'},
                    }
                    
                ]
            },
            {
                path: 'password/forgot',
                component: () => import('pages/guest/password/forgot.vue'),
                meta: {title: 'LaravelAdmin-忘记密码'}
            },
            {
                path: 'password/reset',
                component: () => import('pages/guest/password/reset.vue'),
                meta: {title: 'LaravelAdmin-重置密码'}
            },
            ...errorPages
        ],
        meta: {middleware: ['client','guest']}
    },
    {
        path: '/home',
        component: () => import('pages/home/layout.vue'),
        children: [
            {
                path: '',
                name: 'home',
                component: () => import('pages/home/index.vue'),
                meta: {title: 'LaravelAdmin-主页'}
            },
            {
                path: 'index',
                component: () => import('pages/home/index.vue'),
                meta: {title: 'LaravelAdmin-主页'}
            },
            ...errorPages
        ],
        meta: {middleware: ['client', 'auth']}
    },
    {
        path: '/admin',
        component: () => import('pages/admin/layout.vue'),
        children: [
            {
                path: '',
                component: () => import('pages/admin/index.vue'),
                meta: {title: 'LaravelAdmin-后台主页'}
            },
            {
                path: 'index',
                component: () => import('pages/admin/index.vue'),
                meta: {title: 'LaravelAdmin-后台主页'}
            },
            {
                path: '/:pathMatch(.*)*', // Vue Router 4.x的写法，匹配所有路径
                name: '404',
                component: () => import('pages/error/404.vue'),
                meta: {title: 'LaravelAdmin-页面不存在'}
            },
            ...errorPages
        ],
        meta: {middleware: ['client', 'auth', 'admin']}
    },
    // 404路由配置 - 必须放在所有路由的最后
    {
        path: '/error',
        component: () => import('pages/error/layout.vue'),
        children: [
            ...errorPages
        ],
    },
    {
        path: '/',
        component: () => import('pages/layout.vue'),
        children: [
            {
                path: '',
                component: () => import('pages/index.vue'),
                meta: {title: 'LaravelAdmin-首页'}
            },
            {
                path: 'index',
                component: () => import('pages/index.vue'),
                meta: {title: 'LaravelAdmin-首页'}
            },
            ...errorPages
        ],
        meta: {middleware: ['client']}
    },
]

const routerObj = createRouter({
    history: createWebHashHistory(),
    //createWebHistory(),
    //createMemoryHistory(),
    routes,
})
const pageTitle = document.title;
routerObj.beforeEach(async (to, from) => {
    const title = to.meta.title || pageTitle;
    const middleware = to.meta.middleware;
    const configStore = useConfigStore()
    await configStore.fetchConfig()
    //设置图标
    const logo = configStore.logo
    if(logo){
        document.querySelector('link[rel="icon"]').href = logo
    }
    const localConfig = useLocalConfigStore()
    let language = localConfig.language || navigator.language;
    if(language){
       await localConfig.changeLanguage(language)
    }
    if (!middleware) {
        document.title = title;
        return true;
    }
    if(middleware.includes('client') && !configStore.clientId){
        return '/error/401'
    }
    const userStore = useUserStore()
    if(
        middleware.includes('auth') ||
        middleware.includes('admin') ||
        middleware.includes('guest')
    ){
        await userStore.fetchUser()
    }
    if((middleware.includes('auth') ||
        middleware.includes('admin')) &&
        !userStore.isLoggedIn
    ){
        return { name: 'login' }
    }

    if(middleware.includes('guest') && userStore.isLoggedIn){
        return { name: 'home' }
    }

    if(
        middleware.includes('admin') && 
        !userStore.isAdmin
    ){
        return '/home/403'
    }
    document.title = title;
})

export const router = routerObj
